import java.util.Arrays;

public class MyArrayList {
    public int []elem;
    public int usedSize;
    public MyArrayList(){
        this.elem=new int[10];
    }

    //打印顺序表
    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    //获取顺序表的有效数据长度
    public int size(){
        return this.usedSize;
    }

    //在pos位置新增元素
    public void add(int pos,int data){
        if(pos<0||pos>usedSize){
            System.out.println("pos位置不合法！");
            return;
        }
        if(isFull()){
            this.elem= Arrays.copyOf(elem,2*elem.length);
        }
        for(int i=usedSize-1;i>=pos;i--){
            elem[i+1]=elem[i];
        }
        elem[pos]=data;
        usedSize++;
    }
    public boolean isFull(){
        return usedSize==elem.length;
    }

    //判定是否包含某个元素
    public boolean contains(int toFind){
        for(int i=0;i<usedSize;i++){
            if(this.elem[i]==toFind){
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置，找不到返回-1
    public int search(int toFind){
        for(int i=0;i<usedSize;i++){
            if(this.elem[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    //获取pos位置的元素
    public int getPos(int pos){
        if(pos<0||pos>=usedSize){
            System.out.println("pos位置不合法！");
            return -1;
        }
        if(isEmpty()){
            System.out.println("顺序表为空！");
            return -1;
        }
        return this.elem[pos];
    }
    public boolean isEmpty(){
        return usedSize==0;
    }

    //给pos位置的元素更新为value
    public void setPos(int pos,int value){
        if(pos<0||pos>=this.usedSize){
            System.out.println("pos位置不合法！");
            return;
        }
        if(isEmpty()){
            System.out.println("顺序表为空！");
            return;
        }
        this.elem[pos]=value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove){
        if(isEmpty()){
            System.out.println("顺序表为空！");
            return;
        }
        int index=search(toRemove);
        if(index==-1){
            System.out.println("没有你要删除的数字");
            return;
        }
        for(int i=index;i<usedSize-1;i++){
             elem[i]=elem[i+1];
        }
        usedSize--;
    }

    //清空顺序表
    public void clear(){
        this.usedSize=0;
    }


    public static void main(String[] args) {
        MyArrayList myArrayList=new MyArrayList();
        myArrayList.add(0,1);
        myArrayList.add(1,2);
        myArrayList.add(2,3);
        myArrayList.add(3,4);
        myArrayList.add(4,5);
        myArrayList.display();
        myArrayList.setPos(0,99);
        myArrayList.setPos(4,99);
        myArrayList.setPos(3,99);
        myArrayList.setPos(99,99);
        myArrayList.display();
        System.out.println("======");
        //myArrayList.clear();
        System.out.println(myArrayList.contains(3));
        System.out.println(myArrayList.getPos(0));

    }
}
